﻿using System;
using System.Collections.Generic;
using System.Net.Mail;
using System.Web.Services;
using StampController.BL;
using StampController.BL.Models;
using StampController.Helpers;

namespace StampController.Pages
{
    public partial class AuthorizationList : BasePage
    {
        private const int PageSize = 20;

        [WebMethod]
        public static object GetAuthorizedList(int pageNum)
        {
            var user = SessionUtils.Get<UsersModel>("USER");

            int count;
            var im = new AuthorizedHistoryImpl();
            IList<AuthorizedHistoryModel> list = im.GetAuthorizedList(user.UserID, pageNum, PageSize, out count);

            var obj = new GridTable<AuthorizedHistoryModel>
            {
                PageNum = pageNum,
                RowCount = count,
                PageSize = PageSize,
                List = list,
                Approval = false
            };

            return obj;
        }

        [WebMethod]
        public static bool DeleteItem(string id)
        {
            var user = SessionUtils.Get<UsersModel>("USER");
            if (user == null)
            {
                return false;
            }
            var im = new AuthorizedHistoryImpl();
            var result = im.SaveAuthorized(new AuthorizedHistoryModel
            {
                ID = new Guid(id),
                ModifiedDate = DateTime.Now,
                ModifiedBy = user.UserID,
                Active = false
            });
            if (result)
            {
                var delegateItem = im.GetAuthorized(new Guid(id));
                return MailHelper.Send(delegateItem.DelegateMail, delegateItem);
            }
            return false;
        }
    }
}